using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Utilities;

namespace Services
{
    public class Particle
    {

        public Particle() { }

        public float Age, AgingSpeed;
        public Vector3 Position, Velocity, Gravity;
        public float MaxAge;
        public float Size;
        public bool IsActive;
        public Color Color;
#warning Kekko weapons
        public float Rotation;
        public Vector2 Scale;


    }

    public class ParticleSystem<T> : CameraConsumer where T : Particle, new()
    {

        string texture_path;
        Texture2D texture;
        List<T> particles = new List<T>(1000);
        ArrayStack<T> free_particles = new ArrayStack<T>(200);

        SpriteBatch spriteBatch;
        BlendState blend_state;

        public ParticleSystem(Game game, string TexturePath)
            : base(game)
        {
            this.texture_path = TexturePath;
            blend_state = BlendState.Additive;
        }

        public ParticleSystem(Game game, string TexturePath, BlendState Blend_State)
            : base(game)
        {
            this.texture_path = TexturePath;
            this.blend_state = Blend_State;
        }

        float size_increase = 0.0f;
        public ParticleSystem(Game game, string TexturePath, BlendState Blend_State, float size_increase)
            : this(game, TexturePath, Blend_State)
        {
            this.size_increase = size_increase;
        }

        public T AddParticle(Action<Particle> init)
        {
            if (free_particles.Count <= 0) free_particles.Push(new T());
            var p = free_particles.Pop();

            init(p);

            particles.Add(p);

            return p;
        }



        protected override void LoadContent()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            texture = Game.Content.Load<Texture2D>(texture_path);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            for (int i = 0; i < particles.Count; i++)
            {
                var part = particles[i];
                if (part.IsActive == false)
                {
                    free_particles.Push(part);
                    particles.RemoveAt(i);
                    i--;
                    continue;
                }
                if (part.Age <= 0.0f)
                {
                    part.IsActive = false;
                }
                part.Age -= particles[i].AgingSpeed * dt;
                part.Position += (particles[i].Velocity + particles[i].Gravity) * dt;
                part.Size += size_increase * dt;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if(spriteBatch==null)
                spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            spriteBatch.Begin(SpriteSortMode.Texture, BlendState.Additive);
            //for_each (var p in particles)
            for (int i = 0; i < particles.Count; i++)
            {
                var p = particles[i];
                if (p.IsActive)
                {
                    var p2d = GraphicsDevice.Viewport.Project(p.Position, projection, view, Matrix.Identity);
                    var size = p.Size * max_depth / Vector3.Distance(p.Position, EyePosition);
                    Vector2 scale = new Vector2(p.Scale.X * size, p.Scale.Y * size);
                    var color = p.Color.ToVector4();
                    if (size_increase != 0.0f)
                        color.W = p.Age / p.MaxAge;
                    spriteBatch.Draw(texture, new Vector2(p2d.X, p2d.Y), null, new Color(color), p.Rotation,
                      new Vector2(texture.Width / 2, texture.Height / 2), scale, SpriteEffects.None, 0.0f);
                }
            }
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void Reset()
        {
            particles.Clear();
        }
    }
}
